import SwiftUI
import SwiftData

// MePageInfoRow
// 我页面信息行

struct MePageInfoRow: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var userInfo: [UserInfo]
    
    @Binding var showAvatarPicker: Bool
    
    var body: some View {
        let avatarURL = URL(string: userInfo.last?.avatarAddress ?? "http://62.234.45.120:1230/static/avatar/default.png")
        let nickNameShow = userInfo.last?.nickName ?? "转转转~"
        let realNameShow = ((userInfo.last?.realNameStage ?? 0) == 2) ?
                            "实名信息：" + (userInfo.last?.realStudentNumber ?? "") + (userInfo.last?.realName ?? "") :
                            "未完成实名认证"

        HStack {
            Button {
                showAvatarPicker.toggle()
            } label: {
                AsyncImage(url: avatarURL) { image in
                    image
                        .resizable()
                        .scaledToFit()
                } placeholder: {
                    ProgressView()
                }
                .frame(width: 100, height: 100)
                .padding(.trailing, 10)
            }
            
            VStack(alignment: .leading) {
                Text(nickNameShow)
                    .bold()
                    .font(.title2)
                
                Text(realNameShow)
                    .font(.footnote)
                    .foregroundColor(.gray)
                    
            }
        }
    }
}

#Preview {
    MePageInfoRow(showAvatarPicker: .constant(false))
}
